-
Notifications
You must be signed in to change notification settings - Fork 6
/
Mouse.vb
323 lines (307 loc) · 12.3 KB
/
Mouse.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
Namespace My
''' <summary>
''' 模拟鼠标操作相关函数
''' </summary>
''' <remarks></remarks>
Partial Public NotInheritable Class Mouse
Private Declare Sub mouse_event Lib "user32.dll" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal dwData As Int32, ByVal dwExtraInfo As UInt32)
<Flags()> _
Private Enum MouseEvent As Int32
Move = 1
LeftButtonDown = 2
LeftButtonUp = 4
RightButtonDown = 8
RightButtonUp = 16
MiddleButtonDown = 32
MiddleButtonUp = 64
Wheel = 2048
AbsoluteLocation = 32768
AbsoluteScale = 65535
End Enum
''' <summary>
''' 移动鼠标位置,位移一定的像素点距离
''' </summary>
''' <param name="x">横向位移(向右为正方向)</param>
''' <param name="y">纵向位移(向下为正方向)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MoveByPixel(ByVal x As Integer, ByVal y As Integer) As Boolean
Try
mouse_event(MouseEvent.Move, x, y, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 移动鼠标位置,位移一定的屏幕百分比
''' </summary>
''' <param name="x">横向位移(百分比,向右为正方向)</param>
''' <param name="y">纵向位移(百分比,向下为正方向)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MoveByPercent(ByVal x As Double, ByVal y As Double) As Boolean
Try
mouse_event(MouseEvent.Move, x * My.Computer.Screen.Bounds.Width, y * My.Computer.Screen.Bounds.Height, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 移动鼠标位置,到指定的像素点坐标
''' </summary>
''' <param name="x">横坐标(原点为屏幕左上角,向右为正方向)</param>
''' <param name="y">纵坐标(原点为屏幕左上角,向下为正方向)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MoveToPixel(ByVal x As Integer, ByVal y As Integer) As Boolean
Try
mouse_event(MouseEvent.Move Or MouseEvent.AbsoluteLocation, x / My.Computer.Screen.Bounds.Width * MouseEvent.AbsoluteScale, y / My.Computer.Screen.Bounds.Height * MouseEvent.AbsoluteScale, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 移动鼠标位置,到指定的屏幕百分比坐标
''' </summary>
''' <param name="x">横坐标(百分比,原点为屏幕左上角,向右为正方向)</param>
''' <param name="y">纵坐标(百分比,原点为屏幕左上角,向下为正方向)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MoveToPercent(ByVal x As Double, ByVal y As Double) As Boolean
Try
mouse_event(MouseEvent.Move Or MouseEvent.AbsoluteLocation, x * MouseEvent.AbsoluteScale, y * MouseEvent.AbsoluteScale, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 获取鼠标位置,像素点坐标值
''' </summary>
''' <returns>结果坐标值(原点为屏幕左上角,X向右为正方向,Y向下为正方向)</returns>
''' <remarks></remarks>
Public Shared Function Position() As Point
Return Windows.Forms.Control.MousePosition
End Function
''' <summary>
''' 获取鼠标位置的显示颜色,像素点Color值
''' </summary>
''' <returns>结果颜色值(System.Drawing.Color)</returns>
''' <remarks></remarks>
Public Shared Function PositionColor() As Color
Dim ScreenArea As Rectangle = My.Computer.Screen.Bounds
Dim Temp As New Bitmap(ScreenArea.Width, ScreenArea.Height)
Dim Graphics As Graphics = Graphics.FromImage(Temp)
Dim Result As Color
Graphics.CopyFromScreen(0, 0, 0, 0, ScreenArea.Size)
Graphics.Dispose()
Result = Temp.GetPixel(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y)
Temp.Dispose()
Return Result
End Function
''' <summary>
''' 移动鼠标位置,到指定的像素点坐标
''' </summary>
''' <param name="Position">位置坐标(原点为屏幕左上角,X向右为正方向,Y向下为正方向)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MoveToPosition(ByVal Position As Point) As Boolean
Try
mouse_event(MouseEvent.Move Or MouseEvent.AbsoluteLocation, Position.X / My.Computer.Screen.Bounds.Width * MouseEvent.AbsoluteScale, Position.Y / My.Computer.Screen.Bounds.Height * MouseEvent.AbsoluteScale, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 按下鼠标左键(保持按下状态)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function LeftDown() As Boolean
Try
mouse_event(MouseEvent.LeftButtonDown, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 释放鼠标左键(取消按下状态)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function LeftUp() As Boolean
Try
mouse_event(MouseEvent.LeftButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标左键单击
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function LeftClick() As Boolean
Try
mouse_event(MouseEvent.LeftButtonDown Or MouseEvent.LeftButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标左键双击
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function LeftDoubleClick() As Boolean
Try
mouse_event(MouseEvent.LeftButtonDown Or MouseEvent.LeftButtonUp, 0, 0, 0, 0)
mouse_event(MouseEvent.LeftButtonDown Or MouseEvent.LeftButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 按下鼠标中键(保持按下状态)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MiddleDown() As Boolean
Try
mouse_event(MouseEvent.MiddleButtonDown, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 释放鼠标中键(取消按下状态)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MiddleUp() As Boolean
Try
mouse_event(MouseEvent.MiddleButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标中键单击
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MiddleClick() As Boolean
Try
mouse_event(MouseEvent.MiddleButtonDown Or MouseEvent.MiddleButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标中键双击
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function MiddleDoubleClick() As Boolean
Try
mouse_event(MouseEvent.MiddleButtonDown Or MouseEvent.MiddleButtonUp, 0, 0, 0, 0)
mouse_event(MouseEvent.MiddleButtonDown Or MouseEvent.MiddleButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 按下鼠标右键(保持按下状态)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function RightDown() As Boolean
Try
mouse_event(MouseEvent.RightButtonDown, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 释放鼠标右键(取消按下状态)
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function RightUp() As Boolean
Try
mouse_event(MouseEvent.RightButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标右键单击
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function RightClick() As Boolean
Try
mouse_event(MouseEvent.RightButtonDown Or MouseEvent.RightButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标右键双击
''' </summary>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function RightDoubleClick() As Boolean
Try
mouse_event(MouseEvent.RightButtonDown Or MouseEvent.RightButtonUp, 0, 0, 0, 0)
mouse_event(MouseEvent.RightButtonDown Or MouseEvent.RightButtonUp, 0, 0, 0, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标滚轮向下滚动
''' </summary>
''' <param name="ScrollValue">滚动距离(单位为像素)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function WheelDown(ByVal ScrollValue As Integer) As Boolean
Try
mouse_event(MouseEvent.Wheel, 0, 0, -ScrollValue, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 鼠标滚轮向上滚动
''' </summary>
''' <param name="ScrollValue">滚动距离(单位为像素)</param>
''' <returns>是否执行成功</returns>
''' <remarks></remarks>
Public Shared Function WheelUp(ByVal ScrollValue As Integer) As Boolean
Try
mouse_event(MouseEvent.Wheel, 0, 0, ScrollValue, 0)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
End Namespace